package com.sunda.spmsoversea.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sunda.spmsoversea.service.IFormatDataToSapService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @program: spms
 * @description: impl
 * @author: Wayne Wu
 * @create: 2021-08-19 10:24
 **/
@Service
public class FormatDataToSapServiceImpl implements IFormatDataToSapService {

    @Override
    public JSONObject formatDataToSap(Map<String, Object> dataHead, List<Map<String, Object>> dataItem, String operation, String type) {
        JSONObject doc = new JSONObject();
        JSONObject request = new JSONObject();
        JSONObject esbAttrs = new JSONObject();
        JSONObject requestData = new JSONObject();
        esbAttrs.put("App_ID", "SPMS");
        esbAttrs.put("Application_ID", "00020000000002");
        esbAttrs.put("Transaction_ID", UUID.randomUUID().toString());

        if (dataHead != null){
            requestData.put("Head", dataHead);
        }
        if (dataItem != null && dataItem.size() > 0){
            requestData.put("Items", dataItem);
        }
        requestData.put("Operation", operation);
        requestData.put("Type", type);

        request.put("ESB_ATTRS", esbAttrs);
        request.put("REQUEST_DATA", requestData);

        doc.put("REQUEST", request);
        return doc;
    }
}
